package com.mobile.collect.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.mobile.collect.R;
import com.mobile.collect.beans.ExportInfo;
import com.mobile.collect.db.EntityManager;
import com.mobile.collect.present.UploadListPresent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListItemLongClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;

/**
 * 类名: ExportListActivity
 * 作者: 李学伟
 * 日期: 2020年09月30日 10:25
 * 描述:
 */
public class UploadListActivity extends BaseListActivity<ExportInfo, UploadListPresent> {
    boolean isEditModel;
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.submitTv)
    Button submitTv;

    @Override
    public int getContentViewId() {
        return R.layout.activity_exportlist;
    }

    @Override
    public void initView() {
        super.initView();
        titleTv.setText("上传");
        submitTv.setText("删除");

        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadmore(false);
        mBaseAdapter.setLongClickListener(new ListItemLongClickListener() {
            @Override
            public void itemLongClick(int position) {
                isEditModel = true;
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        selectList = new ArrayList<>();
        getP().queryList();
    }

    List<ExportInfo> selectList = new ArrayList<>();

    @Override
    public BaseRecyclerAdapter<ExportInfo> getAdapter() {
        return new BaseRecyclerAdapter<ExportInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_upload;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, final int position) {
                super.bindData(holder, position);
                final ExportInfo info = getItemByPosition(position);
                holder.setText(R.id.deviceNameTv, info.getExportTime());
                if (isEditModel) {
                    holder.setVisibility(R.id.selectIv, View.VISIBLE);
                    if (selectList.contains(info)) {
                        holder.setImageResDrawable(R.id.selectIv, R.mipmap.select1);
                    } else {
                        holder.setImageResDrawable(R.id.selectIv, R.mipmap.select0);
                    }
                } else {
                    holder.setVisibility(R.id.selectIv, View.GONE);
                }

            }
        };
    }

    @Override
    public void itemClick(int position) {
        final ExportInfo info = mBaseAdapter.getItemByPosition(position);
        if (isEditModel) {
            if (selectList.contains(info)) {
                selectList.remove(info);
            } else {
                selectList.add(info);
            }
            checkSlelectCount();
        } else {
            UploadActivity.lunch(this, info.get_ID(),info.getExportTime());
        }
    }


    private void checkSlelectCount() {
        if (selectList.size() == mBaseAdapter.getItemCount() && mBaseAdapter.getItemCount() != 0) {
            rightIv.setImageResource(R.mipmap.allselect1);
        } else {
            rightIv.setImageResource(R.mipmap.allselect0);
        }
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
            isEditModel = true;
        } else {
            submitTv.setVisibility(View.GONE);
            isEditModel = false;
        }
        mBaseAdapter.notifyDataSetChanged();
    }

    @Override
    public void getDataFromServer() {

    }

    @Override
    public void getIntentData() {

    }

    @Override
    public UploadListPresent newP() {
        return new UploadListPresent();
    }


    @OnClick({R.id.rightIv, R.id.submitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightIv:
                if (selectList.size() == mBaseAdapter.getItemCount()) {
                    selectList.clear();
                } else {
                    selectList.clear();
                    selectList.addAll(mBaseAdapter.getmList());
                }
                checkSlelectCount();
                break;
            case R.id.submitTv:
                if (selectList.size() == 0) {
                    showToast("请选择的信息");
                    break;
                } else {
                    EntityManager.getInstance().delExportInfo(selectList);
                    mBaseAdapter.remove(selectList);
                }
        }
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @Override
    public void onBackPressed() {
        if (isEditModel) {
            isEditModel = false;
            selectList.clear();
            checkSlelectCount();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }
}
